package com.example.zhu.controller.order;

import com.example.zhu.Mapper.*;
import com.example.zhu.VO.EmployeeVO.EmployeeVO;
import com.example.zhu.VO.OrderVop.OrderVop;
import com.example.zhu.VO.RoomVo.RoomVO;
import com.example.zhu.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.ParameterResolutionDelegate;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/api/order")
@Slf4j
public class orderController {
    @Autowired
    private CommodityMapper commodityMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private OrderProjectMapper orderProjectMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private MemberRecordMapper memberRecordMapper;
    @PostMapping("/add")
    public Reply add(@RequestBody OrderPO orderPO){
        if (orderPO.getName()==null) {
            return Reply.failed();
        }
        Order order = new Order();
        order.setId(String.valueOf(System.currentTimeMillis()));
        order.setRoomName(orderPO.getName());
        order.setStatus(0);
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        List<OrderProject> orderProjects =new ArrayList<>();
        orderPO.getCommodities().forEach(commodity -> {
            OrderProject orderProject = new OrderProject();
            Commodity NewCommodity = commodityMapper.selectById(commodity);
            orderProject.setTotalPrices(commodity.getCount()*NewCommodity.getAmount());
            orderProject.setAmount(NewCommodity.getAmount());
            orderProject.setCount(commodity.getCount());
            orderProject.setName(NewCommodity.getName());
            orderProject.setNickname(commodity.getEmployeeIng());
            orderProject.setEmployeeType(commodity.getEmployeeType());
            orderProject.setOrderId(order.getId());
            Employee NewEmployee = employeeMapper.selectByNickName(commodity.getEmployeeIng());
            NewEmployee.setStatus(2);
            employeeMapper.updataById(NewEmployee);
            orderProjects.add(orderProject);
        });
        for (OrderProject orderProject : orderProjects) {
            orderProjectMapper.insert(orderProject);
        }
        orderMapper.insert(order);
        RoomVO roomVO = new RoomVO();
        roomVO.setName(orderPO.getName());
        RoomVO NewRoomVO = roomMapper.selectName(roomVO);
        if(NewRoomVO.getFloor().equals("1")){
            if(NewRoomVO.getOrderId()==null){
                NewRoomVO.setOrderId("");
            }
            NewRoomVO.setOrderId(NewRoomVO.getOrderId()+order.getId()+" ");
            roomMapper.updataStatus(orderPO.getName(),0,NewRoomVO.getOrderId());
            return Reply.success();
        }
        roomMapper.updataStatus(orderPO.getName(),1,order.getId());
        return Reply.success();

    }
    @PostMapping("/update")
    public Reply update(@RequestBody OrderPO orderPO){
        if (orderPO.getName()==null) {
            return Reply.failed();
        }
        RoomVO roomVO = new RoomVO();
        roomVO.setName(orderPO.getName());
        RoomVO roomVO1 = roomMapper.selectName(roomVO);
        Order order =  orderMapper.selectByid(roomVO1.getOrderId());
        order.setUpdateTime(new Date());
        List<OrderProject> orderProjects =new ArrayList<>();
        orderPO.getCommodities().forEach(commodity -> {
            OrderProject orderProject = new OrderProject();
            Commodity NewCommodity = commodityMapper.selectById(commodity);
            orderProject.setTotalPrices(commodity.getCount()*NewCommodity.getAmount());
            orderProject.setAmount(NewCommodity.getAmount());
            orderProject.setCount(commodity.getCount());
            orderProject.setName(NewCommodity.getName());
            orderProject.setOrderId(order.getId());
            orderProject.setNickname(commodity.getEmployeeIng());
            orderProject.setEmployeeType(commodity.getEmployeeType());
            employeeMapper.updataByNickname(commodity.getEmployeeIng(),1);
            orderProjects.add(orderProject);
        });
        orderProjectMapper.deleteByOrderId(order.getId());
        for (OrderProject orderProject : orderProjects) {
            orderProjectMapper.insert(orderProject);
        }
        orderMapper.updateNickname(order);
        return Reply.success();
    }
    @PostMapping("/cancel")
    public Reply cancel(@RequestBody OrderPO orderPO){
        if (orderPO.getName()==null) {
            return Reply.failed();
        }
        RoomVO roomVO = new RoomVO();
        roomVO.setName(orderPO.getName());
        RoomVO roomVO1 = roomMapper.selectName(roomVO);
        Order order =  orderMapper.selectByid(roomVO1.getOrderId());
        order.setUpdateTime(new Date());
        List<OrderProject> orderProjects = orderProjectMapper.selectList(order.getId());
        for (OrderProject orderProject : orderProjects) {
            employeeMapper.updataByNickname(orderProject.getNickname(),1);
        }
        roomMapper.updataStatus(orderPO.getName(),0,"");
        order.setStatus(-1);
        orderMapper.updateStatus(order);
        return Reply.success();

    }
    @PostMapping("/project")
    public Reply project(@RequestBody OrderPO orderPO){
        if (orderPO.getName()==null) {
            return Reply.failed();
        }
        RoomVO roomVO = new RoomVO();
        roomVO.setName(orderPO.getName());
        RoomVO roomVO1 = roomMapper.selectName(roomVO);
        List<OrderProject> orderProjects = orderProjectMapper.selectList(roomVO1.getOrderId());
        Integer totalPrice=0;
        for (OrderProject orderProject : orderProjects) {
            totalPrice = totalPrice+orderProject.getTotalPrices();
        }
        OrderProject orderProject = new OrderProject();
        orderProject.setTotalPrices(totalPrice);
        orderProject.setName("总价");
        orderProjects.add(orderProject);
        return Reply.success(orderProjects);
    }
    @PostMapping("/end")
    public Reply end(@RequestBody OrderPO orderPO){
        Order order =new Order();
        if (orderPO.getName()!=null) {
            RoomVO roomVO = new RoomVO();
            roomVO.setName(orderPO.getName());
            RoomVO roomVO1 = roomMapper.selectName(roomVO);
            order =  orderMapper.selectByid(roomVO1.getOrderId());
            roomMapper.updataStatus(orderPO.getName(),0,"");
        } else if (orderPO.getOrderId()!=null) {
            order =  orderMapper.selectByid(orderPO.getOrderId());
            String orderId = orderPO.getOrderId();
            orderId="%"+orderId+"%";
            RoomVO roomVO  =roomMapper.selectByOrderId(orderId);
            String OrderReplace = roomVO.getOrderId().replace(orderPO.getOrderId() + " ", "");
            roomMapper.updataStatus(roomVO.getName(),0,OrderReplace);

        }
        List<OrderProject> orderProjects = orderProjectMapper.selectList(order.getId());
        for (OrderProject orderProject : orderProjects) {
            employeeMapper.updataByNickname(orderProject.getNickname(),1);
        }
        if(orderPO.getPayMethod().equals("会员卡扣款")){
            String vipId = orderPO.getVipId();
            if(vipId!=null&&vipId.length()!=0){
                Member member = new Member();
                member.setId(vipId);
                Member Newmember = memberMapper.selectById(member);
                Integer userMoney = Newmember.getUserMoney();
                Integer  NewUserMoney=userMoney-orderPO.getAmount();
                if(NewUserMoney<0) {
                    NewUserMoney = 0;
                }
                MemberRecord memberRecord = new MemberRecord();
                memberRecord.setMemberId(Newmember.getId());
                memberRecord.setMoneyBefore(userMoney);
                memberRecord.setMoneyAfter(NewUserMoney);
                memberRecord.setMoneyType("-"+orderPO.getAmount()+"元");
                memberRecord.setMoneyDate(new Date());
                memberRecord.setType("消费");
                memberRecord.setOrderId(order.getId());
                memberRecordMapper.Insert(memberRecord);
                Newmember.setUserMoney(NewUserMoney);
                Newmember.setFrozenMoney(Newmember.getFrozenMoney()+orderPO.getAmount());
                Newmember.setUpdateTime(new Date());
                memberMapper.updataById(Newmember);
            }
        }
        order.setUpdateTime(new Date());
        order.setPay(orderPO.getAmount());
        order.setStatus(1);
        order.setMsg(orderPO.getMsg());
        order.setPayMethod(orderPO.getPayMethod());
        orderMapper.updatePay(order);
        return Reply.success();
    }
    @PostMapping("/my")
    public Reply my(@RequestBody Order order){
        String nickname = order.getNickname();
        Integer status = order.getStatus();
        Date beginTime = order.getBeginTime();
        Date endTime = order.getEndTime();
        MyPage page = order.getPage();
        page.setMin((page.getPage()-1)*page.getSize());
        page.setMax((page.getPage())*page.getSize());
        List<Order> OrderAllList;
        if(status!=null){
            String Status="%"+status+"%";
            OrderAllList = orderMapper.getStatusList(Status);
            OrderVop orderVop = new OrderVop();
            orderVop.setRecords(OrderAllList);
            return Reply.success(orderVop);
        }else if(nickname!=null&&nickname.length()!=0){
            String nicknames="%"+nickname+"%";
            OrderAllList = orderMapper.getNickNameList(nicknames);
            OrderVop orderVop = new OrderVop();
            orderVop.setRecords(OrderAllList);
            return Reply.success(orderVop);
        }else if(endTime!=null&&beginTime!=null){
            OrderAllList = orderMapper.getDateList(beginTime,endTime);
            OrderVop orderVop = new OrderVop();
            orderVop.setRecords(OrderAllList);
            return Reply.success(orderVop);
        }else{
            OrderAllList = orderMapper.getAllList(page);
        }
        OrderVop orderVop = new OrderVop();
        orderVop.setRecords(OrderAllList);
        orderVop.setSize(page.getSize()+"");
        orderVop.setCurrent(page.getPage()+"");
        Integer allListCount = Integer.valueOf(orderMapper.getAllListCount());
        orderVop.setTotal(allListCount);
        orderVop.setSearchCount(true);
        orderVop.setPages(String.valueOf(allListCount/page.getSize()+1));
        return Reply.success(orderVop);
    }
    @GetMapping("/project/{id}")
    public Reply  delete(@PathVariable(name = "id") String id){
        List<OrderProject> orderProjects = orderProjectMapper.selectList(id);
        return Reply.success(orderProjects);
    }
    @GetMapping("/income/{day}")
    public Reply  income(@PathVariable(name = "day") Integer day){
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, -day);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MILLISECOND, 0);
        Date date = c.getTime();
        List<IncomeVo> incomeVos = orderMapper.selectNumList(date);
        for (IncomeVo incomeVo : incomeVos) {
            incomeVo.setName(incomeVo.getDate());
            incomeVo.setValue(incomeVo.getNum());
        }
        return Reply.success(incomeVos);
    }
    @GetMapping("/orderNum/{day}")
    public Reply  orderNum(@PathVariable(name = "day") Integer day){
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, -day);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MILLISECOND, 0);
        Date date = c.getTime();
        List<IncomeVo> incomeVos = orderMapper.selectCountList(date);
        for (IncomeVo incomeVo : incomeVos) {
            incomeVo.setName(incomeVo.getDate());
            incomeVo.setValue(incomeVo.getNum());
        }
        return Reply.success(incomeVos);
    }
    @PostMapping("/floor")
    public Reply floor(@RequestBody OrderPO orderPO){
        RoomVO roomVO = new RoomVO();
        roomVO.setName(orderPO.getName());
        RoomVO roomVO1 = roomMapper.selectName(roomVO);
        String[] orderIds = roomVO1.getOrderId().split(" ");
        List<Order> OrderAllList=new ArrayList<>();
        for (String orderId : orderIds) {
            Order order = orderMapper.selectByid(orderId);
            if(order!=null){
                OrderAllList.add(order);
            }
        }
        OrderVop orderVop = new OrderVop();
        orderVop.setRecords(OrderAllList);
        return Reply.success(orderVop);
    }
    @PostMapping("/cancelId")
    public Reply cancelId(@RequestBody OrderPO orderPO){
        Order order =  orderMapper.selectByid(orderPO.getOrderId());
        order.setUpdateTime(new Date());
        List<OrderProject> orderProjects = orderProjectMapper.selectList(order.getId());
        for (OrderProject orderProject : orderProjects) {
            employeeMapper.updataByNickname(orderProject.getNickname(),1);
        }
        RoomVO roomVO = new RoomVO();
        roomVO.setName(orderPO.getName());
        RoomVO NewRoomVO = roomMapper.selectName(roomVO);
        String NewOrderId = NewRoomVO.getOrderId().replace(order.getId()+" ", "");
        roomMapper.updataStatus(roomVO.getName(),0,NewOrderId);
        order.setStatus(-1);
        orderMapper.updateStatus(order);
        return Reply.success();

    }
    @PostMapping("/updateId")
    public Reply updateId(@RequestBody OrderPO orderPO){
        Order order =  orderMapper.selectByid(orderPO.getOrderId());
        order.setUpdateTime(new Date());
        List<OrderProject> orderProjects =new ArrayList<>();
        orderPO.getCommodities().forEach(commodity -> {
            OrderProject orderProject = new OrderProject();
            Commodity NewCommodity = commodityMapper.selectById(commodity);
            orderProject.setTotalPrices(commodity.getCount()*NewCommodity.getAmount());
            orderProject.setAmount(NewCommodity.getAmount());
            orderProject.setCount(commodity.getCount());
            orderProject.setName(NewCommodity.getName());
            orderProject.setOrderId(order.getId());
            orderProject.setNickname(commodity.getEmployeeIng());
            orderProject.setEmployeeType(commodity.getEmployeeType());
            employeeMapper.updataByNickname(commodity.getEmployeeIng(),1);
            orderProjects.add(orderProject);
        });
        orderProjectMapper.deleteByOrderId(order.getId());
        for (OrderProject orderProject : orderProjects) {
            orderProjectMapper.insert(orderProject);
        }
        orderMapper.updateNickname(order);
        return Reply.success();
    }
    @PostMapping("/projectId")
    public Reply projectId(@RequestBody OrderPO orderPO){
        List<OrderProject> orderProjects = orderProjectMapper.selectList(orderPO.getOrderId());
        Integer totalPrice=0;
        for (OrderProject orderProject : orderProjects) {
            totalPrice = totalPrice+orderProject.getTotalPrices();
        }
        OrderProject orderProject = new OrderProject();
        orderProject.setTotalPrices(totalPrice);
        orderProject.setName("总价");
        orderProjects.add(orderProject);
        return Reply.success(orderProjects);
    }
}
